package com.xca.znb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xca.znb.R;
import com.xca.znb.entity.Goods;
import com.xca.znb.entity.News;

import java.util.List;

public class SchoolFragmentAdapter extends BaseAdapter {
    private Context context ;
    private List<News> newsList ;
    public SchoolFragmentAdapter(Context context , List<News> newsList){
        this.context = context ;
        this.newsList = newsList ;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.school_news_item , parent , false);
            viewHolder = new ViewHolder() ;
            viewHolder.showTitle = convertView.findViewById(R.id.school_news_list_titleText);
            viewHolder.showId =  convertView.findViewById(R.id.school_news_list_id);
            viewHolder.showTime = convertView.findViewById(R.id.school_news_list_publishTime);
            viewHolder.showImg = convertView.findViewById(R.id.school_news_list_titleImg) ;
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        News news = newsList.get(position);
        viewHolder.showTitle.setText(news.getFirstTitle());

        int id = news.getId() ;
        String newsId = String.valueOf(id);
        viewHolder.showId.setText(newsId);
        viewHolder.showTime.setText(news.getTime());

        Bitmap bitmap = stringPathToBitmap(news.getImg());
        if (bitmap != null){
            viewHolder.showImg.setImageBitmap(bitmap);
        }else {
            viewHolder.showImg.setImageResource(R.drawable.add_photo);
        }
        viewHolder.showId.setTag(position);
        return convertView;
    }

    static class ViewHolder{
        TextView showTitle ;
        TextView showId ;
        TextView showTime ;
        ImageView showImg ;
    }

    public Bitmap stringPathToBitmap(String imagePath) {
        try {
            // 使用BitmapFactory.decodeFile方法将图片路径转换为Bitmap对象
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

            if (bitmap != null) {
                return bitmap;
            } else {
                // 图片加载失败处理
                // Log.e("ImageLoadError", "Failed to load image from path: " + imagePath);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 异常处理
            return null;
        }
    }
}